{extend name="app/shop/view/base.html"/}
{block name="resources"}
<style>
	.good-name, .good-price {
		line-height: 34px;
	}
</style>
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>管理员可以在此页添加电子面单模板</li>
		</ul>
	</div>
</div>

<div class="layui-form ns-form">

	<div class="layui-card ns-card-common ns-card-brief">
		<div class="layui-card-header">
			<span class="ns-card-title">基础设置</span>
		</div>

		<div class="layui-card-body">

			<div class="layui-form-item">
				<label class="layui-form-label"><span class="required">*</span>电子面单名称：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="template_name" lay-verify="required" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>

			<div class="layui-form-item express_company">
				<label class="layui-form-label"><span class="required">*</span>快递公司：</label>
				<div class="layui-input-block ns-len-short">
					<select name="company_id" lay-verify="required" lay-filter="company_id">
						<option value="">请选择</option>
						{foreach $company_list as $v}
						<option value="{$v.company_id}" class="company_id_{$v.company_id}" data-value="{$v.print_style}">{$v.company_name}</option>
						{/foreach}
					</select>
				</div>
			</div>

			<div class="layui-form-item print_style">

			</div>

		</div>
	</div>

	<div class="layui-card ns-card-common ns-card-brief">
		<div class="layui-card-header">
			<span class="ns-card-title">其他设置</span>
		</div>

		<div class="layui-card-body">

			<div class="layui-form-item">
				<label class="layui-form-label">CustomerName：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="customer_name" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">CustomerPwd：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="customer_pwd" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">SendSite：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="send_site" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">SendStaff：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="send_staff" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">MonthCode：</label>
				<div class="layui-input-block">
					<input type="text" value="" name="month_code" autocomplete="off" class="layui-input ns-len-long">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">邮费支付方式：</label>
				<div class="layui-input-block ns-len-short">
					<select name="postage_payment_method">
						<option value="">请选择</option>
						<option value="1">现付</option>
						<option value="2">到付</option>
						<option value="3">月结</option>
					</select>
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">是否上门揽件：</label>
				<div class="layui-input-inline">
					<input type="radio" name="is_notice" value="1" title="是" checked="">
					<input type="radio" name="is_notice" value="0" title="否">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">是否默认：</label>
				<div class="layui-input-inline">
					<input type="radio" name="is_default" value="1" title="是" checked="">
					<input type="radio" name="is_default" value="0" title="否">
				</div>
			</div>

		</div>
	</div>

	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</div>
{/block}
{block name="script"}
<script>

    layui.use(['form', 'laydate'], function() {
        var form = layui.form,
            laydate = layui.laydate,
            repeat_flag = false;

		form.render();

        /**
         * 表单验证
         */
        form.verify({
            time: function(value) {
                var now_time = (new Date()).getTime();
                var start_time = (new Date($("#start_time").val())).getTime();
                var end_time = (new Date(value)).getTime();
                if (now_time > end_time) {
                    return '结束时间不能小于当前时间!'
                }
                if (start_time > end_time) {
                    return '结束时间不能小于开始时间!';
                }
            },
            flnum: function(value) {
                var arrMen = value.split(".");
                var val = 0;
                if (arrMen.length == 2) {
                    val = arrMen[1];
                }
                if (val.length > 2) {
                    return '保留小数点后两位！'
                }
            },
            int: function(value) {
                if (value <= 1 || value % 1 != 0) {
                    return '请输入大于1的正整数！'
                }
            }
        });


        form.on('select(company_id)', function(data){
            var print_style = $(".company_id_"+data.value).attr("data-value");

            print_style = JSON.parse(print_style);

            var html = '<div class="layui-form-item">' +
				'<label class="layui-form-label"><span class="required">*</span>模板样式：</label>' +
				'<div class="layui-input-block ns-len-short">' +
				'<select name="print_style" lay-verify="required">' +
				'<option value="">请选择</option>';

            for(i = 0; i < print_style.length; i ++){
				html += '<option value="'+ i +'">'+ print_style[i]['template_name'] +'</option>';
            }

            html += '</select>' +
				'</div>' +
				'</div>';

			$('.print_style').html(html);
            form.render();

        });

        /**
         * 表单提交
         */
        form.on('submit(save)', function(data){

            if(repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                type: 'POST',
                dataType: 'JSON',
                url: ns.url("electronicsheet://shop/electronicsheet/add"),
                data: data.field,
                async: false,
                success: function(res){
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('添加成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续添加'],
                            yes: function(){
                                location.href = ns.url("electronicsheet://shop/electronicsheet/lists");
                            },
                            btn2: function() {
                                location.href = ns.url("electronicsheet://shop/electronicsheet/add");
                            }
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            })
        });
    });

    function back() {
        location.href = ns.url("electronicsheet://shop/electronicsheet/lists");
    }
</script>
{/block}